#include "CenterGameRecordManager.h"
#include "StringConverter.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "json/json.h"
#include "NGLog.h"
#include "Character.h"
#include "CharManager.h"
#include "Items.h"
#include "ItemManager.h"
#include "Users.h"
#include "UserManager.h"
#include "Channels.h"
#include "ChannelManager.h"
#include "Towns.h"
#include "TownManager.h"
#include "ItemModelManager.h"
#include "Message.h"
#include "CenterBankManager.h"
#include "VipCardManager.h"
#include "Tools.h"
#include "XMLData.h"
#include "GameDefine.h"
#include XMLDATA_MGR_PATH
#include LOGIC_MGR_PATH
#include LOG_MGR_PATH
#include PROTOCOLS_MGR_PATH
#include "OpcodesEx.h"
#include "WorldPacket.h"
#include "AIInterface.h"
#include "WSSocketManager.h"
#include "json/json.h"
#include "ProtocolDealEnums.h"
#include "url.h"


//=============================游戏记录数据库相关操作===================================
bool CenterGameRecordManager::AddCenterGameRecord(CenterGameRecord & record)
{
	Fields fields;
	// sDataMgr.initData(DataManager::DatabaseTypeCenter, DB_NAME_GAMERECORD, &fields);
	sDataMgr.initData(DataManager::DatabaseTypeInfo, DB_NAME_GAMERECORD, &fields);
	
	fields["group_id"] = 1;
	fields["server_id"] = record.server_id;
	fields["status"] = record.status;
	fields["type"] = record.type;
	fields["sub_type"] = record.sub_type;
	fields["flag"] = record.flag;
	fields["game_id"] = record.game_id;
	fields["gz_id"] = record.gz_id;
	fields["town_id"] = record.town_id;
	fields["channel_id"] = record.channel_id;
	fields["start_time"] = record.start_time;
	fields["play_time"] = record.play_time;
	
	fields["init_data"] = record.init_data;
	fields["step_data"] = record.step_data;
	fields["calc_data"] = record.calc_data;
	
	fields["data1"] = record.data1;
	fields["data2"] = record.data2;
	fields["data3"] = record.data3;
	fields["data4"] = record.data4;
	fields["data5"] = record.data5;
	fields["data6"] = record.data6;
	fields["data7"] = record.data7;
	fields["data8"] = record.data8;
	
	fields["udata1"] = record.udata1;
	fields["udata2"] = record.udata2;
	fields["udata3"] = record.udata3;
	fields["udata4"] = record.udata4;
	fields["udata5"] = record.udata5;
	fields["udata6"] = record.udata6;
	fields["udata7"] = record.udata7;
	fields["udata8"] = record.udata8;
	
	fields["bdata1"] = record.bdata1;
	fields["bdata2"] = record.bdata2;
	fields["bdata3"] = record.bdata3;
	fields["bdata4"] = record.bdata4;
	
	fields["ubdata1"] = record.ubdata1;
	fields["ubdata2"] = record.ubdata2;
	fields["ubdata3"] = record.ubdata3;
	fields["ubdata4"] = record.ubdata4;
	
	fields["fdata1"] = record.fdata1;
	fields["fdata2"] = record.fdata2;
	fields["fdata3"] = record.fdata3;
	fields["fdata4"] = record.fdata4;
	
	fields["sdata1"] = record.sdata1;
	fields["sdata2"] = record.sdata2;
	fields["sdata3"] = record.sdata3;
	fields["sdata4"] = record.sdata4;
	fields["sdata5"] = record.sdata5;
	fields["sdata6"] = record.sdata6;
	fields["sdata7"] = record.sdata7;
	fields["sdata8"] = record.sdata8;
	
	fields["user_data"] = record.user_data;
	fields["config"] = record.config;
	fields["info"] = record.info;
	fields["create_time"] = record.create_time;
	
	// if (!sDataMgr.insertData(DataManager::DatabaseTypeCenter, DB_NAME_GAMERECORD, &fields))
	if (!sDataMgr.insertData(DataManager::DatabaseTypeInfo, DB_NAME_GAMERECORD, &fields))
		return false;
	
	record.record_id = fields["record_id"].GetUInt64();
	return true;
}


bool CenterGameRecordManager::UpdateCenterGameRecord(const CenterGameRecord & record)
{
	Fields fields;
	// fields["record_id"] = record.record_id;
	// fields["group_id"] = 1;
	fields["server_id"] = record.server_id;
	fields["status"] = record.status;
	fields["type"] = record.type;
	fields["sub_type"] = record.sub_type;
	fields["flag"] = record.flag;
	fields["game_id"] = record.game_id;
	fields["gz_id"] = record.gz_id;
	fields["town_id"] = record.town_id;
	fields["channel_id"] = record.channel_id;
	fields["start_time"] = record.start_time;
	fields["play_time"] = record.play_time;
	
	fields["init_data"] = record.init_data;
	fields["step_data"] = record.step_data;
	fields["calc_data"] = record.calc_data;
	
	fields["data1"] = record.data1;
	fields["data2"] = record.data2;
	fields["data3"] = record.data3;
	fields["data4"] = record.data4;
	fields["data5"] = record.data5;
	fields["data6"] = record.data6;
	fields["data7"] = record.data7;
	fields["data8"] = record.data8;
	
	fields["udata1"] = record.udata1;
	fields["udata2"] = record.udata2;
	fields["udata3"] = record.udata3;
	fields["udata4"] = record.udata4;
	fields["udata5"] = record.udata5;
	fields["udata6"] = record.udata6;
	fields["udata7"] = record.udata7;
	fields["udata8"] = record.udata8;
	
	fields["bdata1"] = record.bdata1;
	fields["bdata2"] = record.bdata2;
	fields["bdata3"] = record.bdata3;
	fields["bdata4"] = record.bdata4;
	
	fields["ubdata1"] = record.ubdata1;
	fields["ubdata2"] = record.ubdata2;
	fields["ubdata3"] = record.ubdata3;
	fields["ubdata4"] = record.ubdata4;
	
	fields["fdata1"] = record.fdata1;
	fields["fdata2"] = record.fdata2;
	fields["fdata3"] = record.fdata3;
	fields["fdata4"] = record.fdata4;
	
	fields["sdata1"] = record.sdata1;
	fields["sdata2"] = record.sdata2;
	fields["sdata3"] = record.sdata3;
	fields["sdata4"] = record.sdata4;
	fields["sdata5"] = record.sdata5;
	fields["sdata6"] = record.sdata6;
	fields["sdata7"] = record.sdata7;
	fields["sdata8"] = record.sdata8;
	
	fields["user_data"] = record.user_data;
	fields["config"] = record.config;
	fields["info"] = record.info;
	fields["create_time"] = record.create_time;
	
	// int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMERECORD, &fields, "`record_id` = %u", record.record_id);
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeInfo, DB_NAME_GAMERECORD, &fields, "`record_id` = %u", record.record_id);
	
	return count > 0;
}

#define SQLBUFER_LEN 16384

bool CenterGameRecordManager::UpdateCenterGameRecord(Fields * fields, const char * QueryString, ...)
{
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, QueryString); 
	vsnprintf(sql, SQLBUFER_LEN, QueryString, vlist); 
	va_end(vlist);
	// int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMERECORD, fields, sql);
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeInfo, DB_NAME_GAMERECORD, fields, sql);
	return count > 0;
}

bool CenterGameRecordManager::UpdateCenterGameRecord(const uint64 &record_id, Fields * fields)
{
	CenterGameRecord cb;
	GetCenterGameRecord(record_id, &cb);
	
	// int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMERECORD, fields, "`record_id` = %u", record_id);
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeInfo, DB_NAME_GAMERECORD, fields, "`record_id` = %u", record_id);
	
	return count > 0;
}


bool CenterGameRecordManager::GetCenterGameRecord(const uint64 &record_id, CenterGameRecord * record)
{
	assert (record);
	FieldRows rows;
	// sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMERECORD, &rows, "`record_id` = %u", record_id);
	sDataMgr.getDatas(DataManager::DatabaseTypeInfo, DB_NAME_GAMERECORD, &rows, "`record_id` = %u", record_id);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	record->record_id = fields["record_id"].GetUInt64(); 
	record->group_id = fields["group_id"].GetUInt32(); 
	record->server_id = fields["server_id"].GetUInt32(); 
	record->status = fields["status"].GetUInt32(); 
	record->type = fields["type"].GetUInt32(); 
	record->sub_type = fields["sub_type"].GetUInt32(); 
	record->flag = fields["flag"].GetUInt32(); 
	record->game_id = fields["game_id"].GetUInt32(); 
	record->gz_id = fields["gz_id"].GetUInt32(); 
	record->town_id = fields["town_id"].GetUInt32(); 
	record->channel_id = fields["channel_id"].GetUInt32(); 
	record->start_time = fields["start_time"].GetValue(); 
	
	record->play_time = fields["play_time"].GetUInt32(); 
	record->init_data = fields["init_data"].GetValue();
	record->step_data = fields["step_data"].GetValue();
	record->calc_data = fields["calc_data"].GetValue();
	
	record->data1 = fields["data1"].GetInt32();
	record->data2 = fields["data2"].GetInt32();
	record->data3 = fields["data3"].GetInt32();
	record->data4 = fields["data4"].GetInt32();
	record->data5 = fields["data5"].GetInt32();
	record->data6 = fields["data6"].GetInt32();
	record->data7 = fields["data7"].GetInt32();
	record->data8 = fields["data8"].GetInt32();
	
	record->udata1 = fields["udata1"].GetUInt32();
	record->udata2 = fields["udata2"].GetUInt32();
	record->udata3 = fields["udata3"].GetUInt32();
	record->udata4 = fields["udata4"].GetUInt32();
	record->udata5 = fields["udata5"].GetUInt32();
	record->udata6 = fields["udata6"].GetUInt32();
	record->udata7 = fields["udata7"].GetUInt32();
	record->udata8 = fields["udata8"].GetUInt32();
	
	record->bdata1 = fields["bdata1"].GetInt64();
	record->bdata2 = fields["bdata2"].GetInt64();
	record->bdata3 = fields["bdata3"].GetInt64();
	record->bdata4 = fields["bdata4"].GetInt64();
	
	record->ubdata1 = fields["ubdata1"].GetUInt64();
	record->ubdata2 = fields["ubdata2"].GetUInt64();
	record->ubdata3 = fields["ubdata3"].GetUInt64();
	record->ubdata4 = fields["ubdata4"].GetUInt64();
	
	record->fdata1 = fields["fdata1"].GetFloat();
	record->fdata2 = fields["fdata2"].GetFloat();
	record->fdata3 = fields["fdata3"].GetFloat();
	record->fdata4 = fields["fdata4"].GetFloat();
	
	record->sdata1 = fields["sdata1"].GetValue();
	record->sdata2 = fields["sdata2"].GetValue();
	record->sdata3 = fields["sdata3"].GetValue();
	record->sdata4 = fields["sdata4"].GetValue();
	record->sdata5 = fields["sdata5"].GetValue();
	record->sdata6 = fields["sdata6"].GetValue();
	record->sdata7 = fields["sdata7"].GetValue();
	record->sdata8 = fields["sdata8"].GetValue();
	record->user_data = fields["user_data"].GetValue();
	record->config = fields["config"].GetValue();
	record->info = fields["info"].GetValue();
	record->create_time = fields["create_time"].GetValue();
	return true;
}


uint32 CenterGameRecordManager::GetCenterGameRecordList(std::list<CenterGameRecord> * lstData, const char * query,...)
{
	va_list argp;
	va_start(argp, query);
	lstData->clear();
	FieldRows fieldRows;
	
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, query); 
	vsnprintf(sql, SQLBUFER_LEN, query, vlist); 
	va_end(vlist);
	
	// if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMERECORD, &fieldRows, sql))
	if (!sDataMgr.getDatas(DataManager::DatabaseTypeInfo, DB_NAME_GAMERECORD, &fieldRows, sql))
		return 0;
	
	FieldRows::iterator iter, enditer = fieldRows.end();
	for (iter = fieldRows.begin(); iter != enditer; ++ iter)
	{
		CenterGameRecord record;
		Fields &fields = *iter;
		
		record.record_id = fields["record_id"].GetUInt64(); 
		record.group_id = fields["group_id"].GetUInt32(); 
		record.server_id = fields["server_id"].GetUInt32(); 
		record.status = fields["status"].GetUInt32(); 
		record.type = fields["type"].GetUInt32(); 
		record.sub_type = fields["sub_type"].GetUInt32(); 
		record.flag = fields["flag"].GetUInt32(); 
		record.game_id = fields["game_id"].GetUInt32(); 
		record.gz_id = fields["gz_id"].GetUInt32(); 
		record.town_id = fields["town_id"].GetUInt32(); 
		record.channel_id = fields["channel_id"].GetUInt32(); 
		record.start_time = fields["start_time"].GetValue(); 
		
		record.play_time = fields["play_time"].GetUInt32(); 
		record.init_data = fields["init_data"].GetValue();
		record.step_data = fields["step_data"].GetValue();
		record.calc_data = fields["calc_data"].GetValue();
		
		record.data1 = fields["data1"].GetInt32();
		record.data2 = fields["data2"].GetInt32();
		record.data3 = fields["data3"].GetInt32();
		record.data4 = fields["data4"].GetInt32();
		record.data5 = fields["data5"].GetInt32();
		record.data6 = fields["data6"].GetInt32();
		record.data7 = fields["data7"].GetInt32();
		record.data8 = fields["data8"].GetInt32();
		
		record.udata1 = fields["udata1"].GetUInt32();
		record.udata2 = fields["udata2"].GetUInt32();
		record.udata3 = fields["udata3"].GetUInt32();
		record.udata4 = fields["udata4"].GetUInt32();
		record.udata5 = fields["udata5"].GetUInt32();
		record.udata6 = fields["udata6"].GetUInt32();
		record.udata7 = fields["udata7"].GetUInt32();
		record.udata8 = fields["udata8"].GetUInt32();
		
		record.bdata1 = fields["bdata1"].GetInt64();
		record.bdata2 = fields["bdata2"].GetInt64();
		record.bdata3 = fields["bdata3"].GetInt64();
		record.bdata4 = fields["bdata4"].GetInt64();
		
		record.ubdata1 = fields["ubdata1"].GetUInt64();
		record.ubdata2 = fields["ubdata2"].GetUInt64();
		record.ubdata3 = fields["ubdata3"].GetUInt64();
		record.ubdata4 = fields["ubdata4"].GetUInt64();
		
		record.fdata1 = fields["fdata1"].GetFloat();
		record.fdata2 = fields["fdata2"].GetFloat();
		record.fdata3 = fields["fdata3"].GetFloat();
		record.fdata4 = fields["fdata4"].GetFloat();
		
		record.sdata1 = fields["sdata1"].GetValue();
		record.sdata2 = fields["sdata2"].GetValue();
		record.sdata3 = fields["sdata3"].GetValue();
		record.sdata4 = fields["sdata4"].GetValue();
		record.sdata5 = fields["sdata5"].GetValue();
		record.sdata6 = fields["sdata6"].GetValue();
		record.sdata7 = fields["sdata7"].GetValue();
		record.sdata8 = fields["sdata8"].GetValue();
		record.user_data = fields["user_data"].GetValue();
		record.config = fields["config"].GetValue();
		record.info = fields["info"].GetValue();
		record.create_time = fields["create_time"].GetValue();
		
		lstData->push_back(record);
	}
	
	return lstData->size();
}

//=============================游戏记录数据库相关操作===================================

//==============================游戏记录逻辑相关操作====================================
// 存取游戏记录数据
bool CenterGameRecordManager::CreateCenterGameRecord(stCenterGameRecordParma & record_parma)
{
	CenterGameRecord record;
	
	record.group_id = sChannelMgr.getGroupID();
	record.server_id = sChannelMgr.getServerID();
	
	record.type = record_parma.type;
	record.sub_type = record_parma.sub_type;
	
	record.game_id = record_parma.game_id;
	record.gz_id = record_parma.gz_id;
	record.town_id = record_parma.town_id;
	record.channel_id = record_parma.channel_id;
	
	record.init_data = record_parma.init_data;
	record.step_data = record_parma.step_data;
	record.calc_data = record_parma.calc_data;
	record.start_time = record_parma.start_time;	// 游戏开始时间
	
	record.play_time = record_parma.play_time;		// 游戏用时
	record.user_data = record_parma.user_data;		// 用户配置
	record.config = record_parma.config;			// 配置信息
	record.info = record_parma.info;				// 备注信息
	record.create_time = sGLMgr.GetCurDateTime();	// 创建时间
	
	return AddCenterGameRecord(record);
}

//=============================游戏记录逻辑相关操作===================================










// 添加游戏统计信息
bool GameCountInfoManager::AddGameCountInfo(stGameCountInfo & stGCI)
{
	Fields fields;
	sDataMgr.initData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECOUNTINFO, &fields);
	
	fields["agent_id"] = stGCI.agent_id;
	fields["status"] = stGCI.status;
	fields["pid"] = stGCI.pid;
	fields["type"] = stGCI.type;
	
	fields["data1"] = stGCI.data1;
	fields["data2"] = stGCI.data2;
	fields["data3"] = stGCI.data3;
	fields["data4"] = stGCI.data4;
	fields["data5"] = stGCI.data5;
	fields["data6"] = stGCI.data6;
	fields["data7"] = stGCI.data7;
	fields["data8"] = stGCI.data8;
	fields["data9"] = stGCI.data9;
	fields["data10"] = stGCI.data10;
	fields["data11"] = stGCI.data11;
	fields["data12"] = stGCI.data12;
	
	fields["fdata1"] = stGCI.fdata1;
	fields["fdata2"] = stGCI.fdata2;
	fields["fdata3"] = stGCI.fdata3;
	fields["fdata4"] = stGCI.fdata4;
	fields["fdata5"] = stGCI.fdata5;
	fields["fdata6"] = stGCI.fdata6;
	fields["fdata7"] = stGCI.fdata7;
	fields["fdata8"] = stGCI.fdata8;
	
	if (!sDataMgr.insertData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECOUNTINFO, &fields))
		return false;
	
	stGCI.data_id = fields["data_id"].GetUInt32();
	return true;
}

// 更新游戏统计信息
bool GameCountInfoManager::UpdateGameCountInfo(const stGameCountInfo & stGCI)
{
	Fields fields;
	fields["agent_id"] = stGCI.agent_id;
	fields["status"] = stGCI.status;
	fields["pid"] = stGCI.pid;
	fields["type"] = stGCI.type;
	
	fields["data1"] = stGCI.data1;
	fields["data2"] = stGCI.data2;
	fields["data3"] = stGCI.data3;
	fields["data4"] = stGCI.data4;
	fields["data5"] = stGCI.data5;
	fields["data6"] = stGCI.data6;
	fields["data7"] = stGCI.data7;
	fields["data8"] = stGCI.data8;
	fields["data9"] = stGCI.data9;
	fields["data10"] = stGCI.data10;
	fields["data11"] = stGCI.data11;
	fields["data12"] = stGCI.data12;
	
	fields["fdata1"] = stGCI.fdata1;
	fields["fdata2"] = stGCI.fdata2;
	fields["fdata3"] = stGCI.fdata3;
	fields["fdata4"] = stGCI.fdata4;
	fields["fdata5"] = stGCI.fdata5;
	fields["fdata6"] = stGCI.fdata6;
	fields["fdata7"] = stGCI.fdata7;
	fields["fdata8"] = stGCI.fdata8;
	
	fields["sdata1"] = stGCI.sdata1;
	fields["sdata2"] = stGCI.sdata2;
	fields["sdata3"] = stGCI.sdata3;
	fields["sdata4"] = stGCI.sdata4;
	fields["sdata5"] = stGCI.sdata5;
	fields["sdata6"] = stGCI.sdata6;
	fields["sdata7"] = stGCI.sdata7;
	fields["sdata8"] = stGCI.sdata8;
	fields["create_time"] = stGCI.create_time;
	
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECOUNTINFO, &fields, "`data_id` = %u", stGCI.data_id);
	return count > 0;
}


// 获得游戏统计信息
bool GameCountInfoManager::GetGameCountInfo(const uint32 & data_id, stGameCountInfo * stGCI)
{
	assert (stGCI);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECOUNTINFO, &rows, "`data_id` = %u", data_id);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	stGCI->data_id = fields["data_id"].GetUInt32(); 
	stGCI->agent_id = fields["agent_id"].GetUInt32(); 
	stGCI->status = fields["status"].GetUInt32(); 
	stGCI->pid = fields["pid"].GetUInt32(); 
	stGCI->type = fields["type"].GetUInt32(); 
	
	stGCI->data1 = fields["data1"].GetInt32();
	stGCI->data2 = fields["data2"].GetInt32();
	stGCI->data3 = fields["data3"].GetInt32();
	stGCI->data4 = fields["data4"].GetInt32();
	stGCI->data5 = fields["data5"].GetInt32();
	stGCI->data6 = fields["data6"].GetInt32();
	stGCI->data7 = fields["data7"].GetInt32();
	stGCI->data8 = fields["data8"].GetInt32();
	stGCI->data9 = fields["data9"].GetInt32();
	stGCI->data10 = fields["data10"].GetInt32();
	stGCI->data11 = fields["data11"].GetInt32();
	stGCI->data12 = fields["data12"].GetInt32();
	
	stGCI->fdata1 = fields["fdata1"].GetFloat();
	stGCI->fdata2 = fields["fdata2"].GetFloat();
	stGCI->fdata3 = fields["fdata3"].GetFloat();
	stGCI->fdata4 = fields["fdata4"].GetFloat();
	stGCI->fdata5 = fields["fdata5"].GetFloat();
	stGCI->fdata6 = fields["fdata6"].GetFloat();
	stGCI->fdata7 = fields["fdata7"].GetFloat();
	stGCI->fdata8 = fields["fdata8"].GetFloat();
	
	stGCI->sdata1 = fields["sdata1"].GetValue();
	stGCI->sdata2 = fields["sdata2"].GetValue();
	stGCI->sdata3 = fields["sdata3"].GetValue();
	stGCI->sdata4 = fields["sdata4"].GetValue();
	stGCI->sdata5 = fields["sdata5"].GetValue();
	stGCI->sdata6 = fields["sdata6"].GetValue();
	stGCI->sdata7 = fields["sdata7"].GetValue();
	stGCI->sdata8 = fields["sdata8"].GetValue();
	
	stGCI->create_time = fields["create_time"].GetValue();
	return true;
}

// 获得游戏统计信息列表
uint32 GameCountInfoManager::GetGameCountInfoList(std::list<stGameCountInfo> * lstData, const char * query,...)
{
	va_list argp;
	va_start(argp, query);
	lstData->clear();
	FieldRows fieldRows;
	
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, query); 
	vsnprintf(sql, SQLBUFER_LEN, query, vlist); 
	va_end(vlist);
	
	if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECOUNTINFO, &fieldRows, sql))
		return 0;
	
	FieldRows::iterator iter, enditer = fieldRows.end();
	for (iter = fieldRows.begin(); iter != enditer; ++ iter)
	{
		stGameCountInfo stGCI;
		
		Fields &fields = *iter;
		stGCI.data_id = fields["data_id"].GetUInt32(); 
		stGCI.agent_id = fields["agent_id"].GetUInt32(); 
		stGCI.status = fields["status"].GetUInt32(); 
		stGCI.pid = fields["pid"].GetUInt32(); 
		stGCI.type = fields["type"].GetUInt32(); 
		
		stGCI.data1 = fields["data1"].GetInt32();
		stGCI.data2 = fields["data2"].GetInt32();
		stGCI.data3 = fields["data3"].GetInt32();
		stGCI.data4 = fields["data4"].GetInt32();
		stGCI.data5 = fields["data5"].GetInt32();
		stGCI.data6 = fields["data6"].GetInt32();
		stGCI.data7 = fields["data7"].GetInt32();
		stGCI.data8 = fields["data8"].GetInt32();
		stGCI.data9 = fields["data9"].GetInt32();
		stGCI.data10 = fields["data10"].GetInt32();
		stGCI.data11 = fields["data11"].GetInt32();
		stGCI.data12 = fields["data12"].GetInt32();
		
		stGCI.fdata1 = fields["fdata1"].GetFloat();
		stGCI.fdata2 = fields["fdata2"].GetFloat();
		stGCI.fdata3 = fields["fdata3"].GetFloat();
		stGCI.fdata4 = fields["fdata4"].GetFloat();
		stGCI.fdata5 = fields["fdata5"].GetFloat();
		stGCI.fdata6 = fields["fdata6"].GetFloat();
		stGCI.fdata7 = fields["fdata7"].GetFloat();
		stGCI.fdata8 = fields["fdata8"].GetFloat();
		
		stGCI.sdata1 = fields["sdata1"].GetValue();
		stGCI.sdata2 = fields["sdata2"].GetValue();
		stGCI.sdata3 = fields["sdata3"].GetValue();
		stGCI.sdata4 = fields["sdata4"].GetValue();
		stGCI.sdata5 = fields["sdata5"].GetValue();
		stGCI.sdata6 = fields["sdata6"].GetValue();
		stGCI.sdata7 = fields["sdata7"].GetValue();
		stGCI.sdata8 = fields["sdata8"].GetValue();
		
		stGCI.create_time = fields["create_time"].GetValue();
		
		lstData->push_back(stGCI);
	}
	
	return lstData->size();
}

// 获得玩家统计信息
bool GameCountInfoManager::GetGameCountInfo(CharPtr & pChr, stGameCountInfo * stGCI)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	
WGS_GAMECOUNTINFO_LOCK
	assert (stGCI);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECOUNTINFO, &rows, "status < 255 and `pid` = %u and agent_id = %u", pid, agent_id);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	stGCI->data_id = fields["data_id"].GetUInt32(); 
	stGCI->agent_id = fields["agent_id"].GetUInt32(); 
	stGCI->status = fields["status"].GetUInt32(); 
	stGCI->pid = fields["pid"].GetUInt32(); 
	stGCI->type = fields["type"].GetUInt32(); 
	
	stGCI->data1 = fields["data1"].GetInt32();
	stGCI->data2 = fields["data2"].GetInt32();
	stGCI->data3 = fields["data3"].GetInt32();
	stGCI->data4 = fields["data4"].GetInt32();
	stGCI->data5 = fields["data5"].GetInt32();
	stGCI->data6 = fields["data6"].GetInt32();
	stGCI->data7 = fields["data7"].GetInt32();
	stGCI->data8 = fields["data8"].GetInt32();
	stGCI->data9 = fields["data9"].GetInt32();
	stGCI->data10 = fields["data10"].GetInt32();
	stGCI->data11 = fields["data11"].GetInt32();
	stGCI->data12 = fields["data12"].GetInt32();
	
	stGCI->fdata1 = fields["fdata1"].GetFloat();
	stGCI->fdata2 = fields["fdata2"].GetFloat();
	stGCI->fdata3 = fields["fdata3"].GetFloat();
	stGCI->fdata4 = fields["fdata4"].GetFloat();
	stGCI->fdata5 = fields["fdata5"].GetFloat();
	stGCI->fdata6 = fields["fdata6"].GetFloat();
	stGCI->fdata7 = fields["fdata7"].GetFloat();
	stGCI->fdata8 = fields["fdata8"].GetFloat();
	
	stGCI->sdata1 = fields["sdata1"].GetValue();
	stGCI->sdata2 = fields["sdata2"].GetValue();
	stGCI->sdata3 = fields["sdata3"].GetValue();
	stGCI->sdata4 = fields["sdata4"].GetValue();
	stGCI->sdata5 = fields["sdata5"].GetValue();
	stGCI->sdata6 = fields["sdata6"].GetValue();
	stGCI->sdata7 = fields["sdata7"].GetValue();
	stGCI->sdata8 = fields["sdata8"].GetValue();
	
	stGCI->create_time = fields["create_time"].GetValue();
	return true;
}

// 初始化玩家统计数据
stGameCountInfo GameCountInfoManager::InitGameCountInfo(CharPtr & pChr)
{
	stGameCountInfo temp;
	if(pChr.isNull())
		return temp;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return temp;
	
WGS_GAMECOUNTINFO_LOCK
	stGameCountInfo stGCI;
	stGCI.agent_id = pUser->getUInt32Field("reg_from");
	stGCI.pid = pUser->getUInt32Field("platform_id");
	stGCI.status = 1;
	
	stGCI.create_time = sGLMgr.GetCurDateTime();
	AddGameCountInfo(stGCI);
	return stGCI;
}

// 增加游戏局数
bool GameCountInfoManager::AddDDZPlayGameCount(CharPtr & pChr, bool bWin)
{
	if(pChr.isNull())
		return false;
	
	stGameCountInfo stGCI;
	if(!GetGameCountInfo(pChr, &stGCI))
		stGCI = InitGameCountInfo(pChr);
	
	if(stGCI.data_id == 0)
		return false;
	
WGS_GAMECOUNTINFO_LOCK
	stGCI.data1++;
	if(bWin)
		stGCI.data2++;
	
	// 胜率
	stGCI.data10 = uint32(float(stGCI.data2) / float(stGCI.data1) * 10000);
	return UpdateGameCountInfo(stGCI);
}

// 增加比赛次数
bool GameCountInfoManager::AddDDZPlayMatchCount(CharPtr & pChr, const uint32 & match_type)
{
	if(pChr.isNull())
		return false;
	
	stGameCountInfo stGCI;
	if(!GetGameCountInfo(pChr, &stGCI))
		stGCI = InitGameCountInfo(pChr);
	
	if(stGCI.data_id == 0)
		return false;
	
WGS_GAMECOUNTINFO_LOCK
	if(match_type == 1)
	{// 积分赛
		stGCI.data3++;
	}
	else if(match_type == 2)
	{// 淘汰赛
		stGCI.data4++;
	}
	else
	{
		return false;
	}
	
	return UpdateGameCountInfo(stGCI);
}

// 增加比赛分数
bool GameCountInfoManager::AddDDZMatchScore(CharPtr & pChr, const float & score)
{
	if(pChr.isNull())
		return false;
	
	stGameCountInfo stGCI;
	if(!GetGameCountInfo(pChr, &stGCI))
		stGCI = InitGameCountInfo(pChr);
	
	if(stGCI.data_id == 0)
		return false;
	
WGS_GAMECOUNTINFO_LOCK
	
	stGCI.fdata1 += score;
	return UpdateGameCountInfo(stGCI);
}

// 增加冠军次数
bool GameCountInfoManager::AddDDZMatchChampionCount(CharPtr & pChr, const uint32 & match_type)
{
	if(pChr.isNull())
		return false;
	
	stGameCountInfo stGCI;
	if(!GetGameCountInfo(pChr, &stGCI))
		stGCI = InitGameCountInfo(pChr);
	
	if(stGCI.data_id == 0)
		return false;
	
WGS_GAMECOUNTINFO_LOCK
	if(match_type == 1)
	{// 积分赛
		stGCI.data6++;
	}
	else if(match_type == 2)
	{// 淘汰赛
		stGCI.data7++;
	}
	else
	{
		return false;
	}
	
	return UpdateGameCountInfo(stGCI);
}

// 增加获奖次数
bool GameCountInfoManager::AddDDZBountyCount(CharPtr & pChr, const uint32 & match_type)
{
	if(pChr.isNull())
		return false;
	
	stGameCountInfo stGCI;
	if(!GetGameCountInfo(pChr, &stGCI))
		stGCI = InitGameCountInfo(pChr);
	
	if(stGCI.data_id == 0)
		return false;
	
WGS_GAMECOUNTINFO_LOCK
	if(match_type == 1)
	{// 积分赛
		stGCI.data8++;
	}
	else if(match_type == 2)
	{// 淘汰赛
		stGCI.data9++;
	}
	else
	{
		return false;
	}
	
	return UpdateGameCountInfo(stGCI);
}







